/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/Data/Rel/Details/MySQLQueryResult.h"
#include <cassert>

namespace NodeCpp {
namespace Data {
namespace Rel {

    MySQLQueryResult::MySQLQueryResult(MYSQL_RES* _Res, std::uint64_t _Row)
        : QueryResult()
        , Res_(_Res)
    {
        NODECPP_ASSERT(_Res);

        FieldCount_ = mysql_num_fields(_Res);
        if (FieldCount_ == 0) return;
        Row_ = new Field[FieldCount_];
        if (!Row_) return;

        MYSQL_FIELD* _Rfields = mysql_fetch_fields(_Res);
        for (unsigned int _X = 0; _X < FieldCount_; ++_X) {
            Row_[_X].SetType(_ConvertTypes(_Rfields[_X].type));
		}

        RowCount_ = _Row;
    }

    MySQLQueryResult::~MySQLQueryResult(void)
    {
        Close();
    }

    bool MySQLQueryResult::NextRow(void)
    {
        NODECPP_ASSERT(Res_);

        MYSQL_ROW _Row = mysql_fetch_row(Res_);
        if (!_Row) {
            Close();
            return false;
        }

		for (unsigned int _X = 0; _X < FieldCount_; ++_X) {
			Row_[_X].SetValue(_Row[_X]);
        }

        return true;
    }

    void MySQLQueryResult::Close(void)
    {
        if (Res_) {
            mysql_free_result(Res_);
            Res_ = NULL;
        }
        if (Row_) {
            delete[] Row_;
            Row_ = NULL;
        }
    }

    Field::DataTypes MySQLQueryResult::_ConvertTypes(enum_field_types _Type) const
    {
        switch (_Type)
        {
        case FIELD_TYPE_TIMESTAMP:
        case FIELD_TYPE_DATE:
        case FIELD_TYPE_TIME:
        case FIELD_TYPE_DATETIME:
        case FIELD_TYPE_YEAR:
        case FIELD_TYPE_STRING:
        case FIELD_TYPE_VAR_STRING:
        case FIELD_TYPE_BLOB:
        case FIELD_TYPE_SET:
        case FIELD_TYPE_NULL:
            return Field::TYPE_STRING;
        case FIELD_TYPE_TINY:
        case FIELD_TYPE_SHORT:
        case FIELD_TYPE_LONG:
        case FIELD_TYPE_INT24:
        case FIELD_TYPE_LONGLONG:
        case FIELD_TYPE_ENUM:
            return Field::TYPE_INTEGER;
        case FIELD_TYPE_DECIMAL:
        case FIELD_TYPE_FLOAT:
        case FIELD_TYPE_DOUBLE:
            return Field::TYPE_FLOAT;
        default:
            return Field::TYPE_UNKNOWN;
        }
    }

}}}